<template>
  <div class="app-container">
    <PageSearch
      :showSearch.sync="showSearch"
      :getList="getList"
      :handleQuery="handleQuery"
      :resetQuery="resetQuery">
      <template #form>
        <el-form :model="queryParams" ref="queryForm" size="small">
          <el-row>
            <!-- 店铺名称 -->
            <el-form-item prop="shopName">
              <el-input
                :placeholder="$t('店铺名称')"
                v-model="queryParams.shopName"
                clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- 租户名称 -->
            <el-form-item prop="distrName">
              <el-input
                :placeholder="$t('租户名称')"
                v-model="queryParams.distrName"
                clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- 抓取时间 -->
            <el-form-item prop="purchaseDate">
              <el-date-picker
                :placeholder="$t('抓取时间')"
                :start-placeholder="$t('开始时间')"
                :end-placeholder="$t('结束时间')"
                style="width: 400px"
                v-model="queryParams.time"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss" />
            </el-form-item>
          </el-row>
        </el-form>
      </template>
      <template #actions>
        <el-button type="primary" plain @click="handleInstallChromeExt" size="mini">
          {{ $t('chrome插件安装') }}
        </el-button>
      </template>
    </PageSearch>

    <el-table :height="calTableHeight()" ref="mianRef" v-loading="loading" :data="tableData">
      <!-- 列表显示字段： recordsId 记录号 distrName 租户名称 shopName 店铺名称 createTime 抓取时间 -->
      <!-- 记录号 -->
      <el-table-column show-overflow-tooltip label="记录号" align="center" prop="recordsId" />
      <!-- 租户名称 -->
      <el-table-column show-overflow-tooltip label="租户名称" align="center" prop="distrName" />
      <!-- 店铺名称 -->
      <el-table-column show-overflow-tooltip label="店铺名称" align="center" prop="shopName" />
      <!-- 是否同步 -->
      <el-table-column show-overflow-tooltip label="是否同步" align="center" prop="status">
        <template slot-scope="{ row }">
          <!-- isSync 是否同步 0-否 1-是 -->
          <span v-if="row.isSync == 1" style="color: greenyellow">
            {{ $t('是') }}
          </span>
          <span v-if="row.isSync == 0" style="color: red">
            {{ $t('否') }}
          </span>
        </template>
      </el-table-column>

      <!-- 抓取时间 -->
      <el-table-column show-overflow-tooltip label="抓取时间" align="center" prop="createTime" />

      <!-- 操作 -->
      <el-table-column :label="$t('操作')" align="center" fixed="right">
        <template slot-scope="{ row }">
          <el-dropdown @command="(command) => handleCommand(command, row)">
            <span class="el-dropdown-link">
              {{ $t('更多') }}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <!-- isSync 是否同步 0-否 1-是 -->
              <!-- <el-dropdown-item
                v-hasPermi="['distribution:orderMercari:syncData']"
                command="sync"
                :disabled="row.isSync == 1">
                同步
              </el-dropdown-item> -->
              <el-dropdown-item v-hasPermi="['distribution:orderMercari:getList']" command="detail">
                详情
              </el-dropdown-item>
              <el-dropdown-item
                command="delete"
                v-hasPermi="['distribution:orderMercari:deleteOrderMercariIndividualByRecordsId']">
                删除
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 错误信息 -->
    <ErrList :show.sync="errList.show" @getList="getList" :formData="errList.formData" />
    <!-- 详情 -->
    <Detail :show.sync="detail.show" @getList="getList" :formData="detail.formData" />
  </div>
</template>

<script>
import {
  getOrderMercariIndividualRecordsList,
  deleteOrderMercariIndividualByRecordsId,
  syncData,
  checkSku,
} from '@/api/orderManage/mercariPersonalSync'
import ErrList from './components/errList.vue'
import Detail from './components/detail.vue'
export default {
  activated() {
    this.$nextTick(() => this.$refs['mianRef']?.doLayout())
  },
  name: 'MercariPersonalSync',
  components: {
    ErrList,
    Detail,
  },
  data() {
    return {
      /**详情 */
      detail: {
        show: false,
        formData: {},
      },
      /**错误信息 */
      errList: {
        show: false,
        formData: {},
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 供应厂家表格数据
      tableData: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        time: [],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleInstallChromeExt() {
      window.open(
        'https://chromewebstore.google.com/detail/my-extension/nmbicklmfgoplepolhcnkimbmilphohp?hl=zh-CN&authuser=0',
      )
    },
    handleCommand(command, row) {
      console.log(`command ==>`, command)
      switch (command) {
        case 'sync':
          this.handleSync(row)
          break
        case 'detail':
          this.handleDetail(row)
          break
        case 'delete':
          this.handleDelete(row)
          break

        default:
          break
      }
    },
    handleSync(row) {
      checkSku(row.recordsId).then((res) => {
        const { code, data } = res
        if (code === 200 && !data) {
          syncData({ distrId: row.distrId, recordsId: row.recordsId }).then((res) => {
            const { code, data, msg } = res
            if (code === 200) {
              this.$message({ type: 'success', message: msg })
              this.getList()
            }
          })
        } else {
          this.errList.show = true
          this.errList.formData = data
        }
      })
    },
    handleDelete(row) {
      this.$confirm(this.$t('确定删除?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        deleteOrderMercariIndividualByRecordsId(row.recordsId).then((res) => {
          const { code, data, msg } = res
          if (code === 200) {
            this.$message({ type: 'success', message: msg })
            this.getList()
          }
        })
      })
    },
    handleDetail(row) {
      this.detail.show = true
      this.detail.formData = row
    },
    handleddress(row) {
      return (row.prefecture || '') + (row.city || '') + (row.address1 || '') + (row.address2 || '')
    },
    /** 查询供应厂家列表 */
    getList() {
      /****************** 处理查询条件 *********************/
      const selectDate = this.queryParams.time || []
      const createTimeStart = selectDate[0] || ''
      const createTimeEnd = selectDate[1] || ''
      const data = {
        ...this.queryParams,
        createTimeStart,
        createTimeEnd,
      }
      delete data.time
      /**************** 处理查询条件 end *******************/
      this.loading = true
      getOrderMercariIndividualRecordsList(data).then((response) => {
        this.tableData = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = ''))
      this.queryParams.pageSize = 10
      this.queryParams.pageNum = 1
      this.handleQuery()
    },
  },
}
</script>
